package cn.com.checne.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.checne.model.domain.LanguageInfo;
import cn.com.checne.model.domain.PostCategory;


@Repository
public class PostCategoryDao {
	protected static final Logger log = Logger.getLogger(PostCategoryDao.class);

	/**
	 * create
	 * @param cate
	 * @return
	 */
	public boolean create(PostCategory cate) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String createSql = "insert into tbl_post_category(cate_name,cate_desc,cate_order,cate_show_type,lg_id_fk) values(?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(createSql);
			int i = 0 ;
			pstmt.setString(++i,cate.getName());
			pstmt.setString(++i,cate.getDesc());
			pstmt.setInt(++i,cate.getOrder());
			pstmt.setInt(++i,cate.getShowType());
			pstmt.setInt(++i,cate.getLanguageId());
			pstmt.execute();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param cate
	 * @return
	 */
	public boolean update(PostCategory cate) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_post_category set cate_name = ?,cate_desc = ? ,cate_order = ? ,cate_show_type = ?,lg_id_fk = ? where cate_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setString(++i,cate.getName());
			pstmt.setString(++i,cate.getDesc());
			pstmt.setInt(++i,cate.getOrder());
			pstmt.setInt(++i,cate.getShowType());
			pstmt.setInt(++i,cate.getLanguageId());
			pstmt.setInt(++i,cate.getCateId());
			log.info("[SQL]" + pstmt.toString());
			pstmt.executeUpdate();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param id
	 * @return
	 */
	public boolean delete(int id){
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "delete from tbl_post_category where cate_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,id);
			pstmt.executeUpdate();
			log.info("[SQL]" + pstmt.toString());
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result ;
	}
	
	/**
	 * 
	 * @param cateId
	 * @return
	 */
	public PostCategory getPostCategory(int cateId) {
		PostCategory cate = new PostCategory();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_post_category where cate_id = ? ";

			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, cateId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<PostCategory> list = getResult(rs);
			cate = (list != null && list.size() > 0 ) ? list.get(0) : cate ;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cate;
	}
	/**
	 * 
	 * @return
	 */
	public List<PostCategory> getAll(int languageId) {
		List<PostCategory> list = new ArrayList<PostCategory>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_post_category " ;
			if(languageId > 0){
				sql += " where lg_id_fk = ? " ;
			}
			sql += "order by cate_order asc ";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			if(languageId > 0){
				pstmt.setInt(1, languageId);
			}
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			list.addAll(getResult(rs));
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * get rseult from resultSet
	 * @param rs
	 * @return
	 */
	public List<PostCategory> getResult(ResultSet rs) {
		List<PostCategory> list = new ArrayList<PostCategory>();
		try {
			PostCategory cate = null ;
			while(rs.next()) {
				cate = new PostCategory();
				cate.setCateId(rs.getInt("cate_id"));
				cate.setName(rs.getString("cate_name"));
				cate.setDesc(rs.getString("cate_desc"));
				cate.setOrder(rs.getInt("cate_order"));
				cate.setShowType(rs.getInt("cate_show_type"));
				cate.setLanguageId(rs.getInt("lg_id_fk"));
				LanguageInfo lg = new LanguageInfoDao().getLanguageInfoById(cate.getLanguageId());
				cate.setLanguageInfo(lg);
				list.add(cate);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}